package pit.com.ordermobilesystem.fragment;

import java.io.UnsupportedEncodingException;

import org.json.JSONArray;
import org.json.JSONObject;

import com.aviary.android.feather.cds.billing.util.Purchase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import pit.com.ordermobilesystem.BaseFragment;
import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.ShopActivity;
import pit.com.ordermobilesystem.adapter.ShopAdapter;
import pit.com.ordermobilesystem.adapter.StoreAdapter;
import pit.com.ordermobilesystem.http.HttpApiException;
import pit.com.ordermobilesystem.http.OrderSystemHttpService;
import pit.com.ordermobilesystem.sharepreference.OrderSharePreference;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

public class ShopFragment extends BaseFragment{
	View rootView;
	LinearLayout lay_progress;
	ShopAdapter adapter;
	SwipeRefreshLayout swipeLayout;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	    }

	    @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	            Bundle savedInstanceState) {
	         rootView = inflater.inflate(R.layout.fragment_shop, container, false);

	
			ListView lv_shop=(ListView) rootView.findViewById(R.id.lv_shop);
			lv_shop.setOnItemClickListener(new OnItemClickListener() {
	
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					JSONObject data=(JSONObject) adapter.getItem(position);
					Intent intent=new Intent(getActivity(),ShopActivity.class);
					intent.putExtra("data", data.toString());
					OrderSharePreference.setShop(getActivity(), data.toString());
					getActivity().startActivity(intent);
				}
	
				
			});
			lay_progress=(LinearLayout) rootView.findViewById(R.id.lay_progress);
			swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
	        swipeLayout.setOnRefreshListener(new OnRefreshListener() {
				
				@Override
				public void onRefresh() {
					new LoadingShop(TypeLoadingMore.PULL_TO_REFRESH).execute();
					
				}
			});
	        swipeLayout.setColorScheme(android.R.color.holo_blue_bright, 
	                android.R.color.holo_green_light, 
	                android.R.color.holo_orange_light, 
	                android.R.color.holo_red_light);	        
			
			new LoadingShop(TypeLoadingMore.LOADING_FIRST).execute();
			return rootView;
	    }
	    
		class LoadingShop extends AsyncTask<String, Void, Void> {

			TypeLoadingMore typeLoading;
			Exception exception;
			int response_code;
			JSONArray arrayShop;
			
			public LoadingShop(TypeLoadingMore typeLoading) {
				super();
				this.typeLoading = typeLoading;
			}
			protected void onPreExecute() {
				if(typeLoading==TypeLoadingMore.LOADING_FIRST){
					lay_progress.setVisibility(View.VISIBLE);
				}

			}

			protected Void doInBackground(final String... args) {
				OrderSystemHttpService httpService=new OrderSystemHttpService(OrderSharePreference.getHost(getActivity()),OrderSharePreference.getPort(getActivity()));
				try {
					JSONObject data=new JSONObject();
					data.put("limit", "10");
					data.put("offset", 0);
					data.put("type_business",getArguments().getInt("type_business"));
					JSONObject result=httpService.requestToService(OrderSystemHttpService.URL_SELECT_SHOP, data.toString());
					if(result.getString("status").equals("ok")){		
						arrayShop=result.getJSONObject("data").getJSONArray("shops");
					}else{
						exception=new Exception(result.getString("error"));
					}

				} catch (UnsupportedEncodingException e) {
					exception=e;
				} catch (HttpApiException e) {
					exception=e;
				} catch (Exception e) {
					exception=e;
				}
				return null;
			}

			protected void onPostExecute(final Void unused) {
				if(typeLoading==TypeLoadingMore.LOADING_FIRST){
					lay_progress.setVisibility(View.GONE);
				}else if(typeLoading==TypeLoadingMore.PULL_TO_REFRESH){
					swipeLayout.setRefreshing(false);
				}
				
				if(exception!=null){
					showAlert("",exception.getMessage());
				}else{
					if(adapter==null){
						adapter=new ShopAdapter(arrayShop,getActivity());
						ListView lv_shop=(ListView) rootView.findViewById(R.id.lv_shop);
						lv_shop.setAdapter(adapter);
					}else{

					}
					adapter.notifyDataSetChanged();
				}
				
			}

		}	    
}
